import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Set;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import redis.clients.jedis.Jedis;

import com.herohead.redis.GetRedisServer;
import com.herohead.redis.RedisVars;


public class CleanUpRemoteRedis {

	static Jedis jd;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		GetRedisServer grs = new GetRedisServer();

		//String redisHostName = grs.getRedisHost();
		RedisVars.redisHost = grs.getRedisHost();
		//RedisVars.redisHost = "172.21.5.41";


		jd = new Jedis(RedisVars.redisHost,6380);

		try {
			if (!grs.checkForRedis(jd)) {
				System.out.println("redis server not found");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}




		JSONParser parser = new JSONParser();
		Object obj = null;
		try {
			obj = parser.parse(getValue("zrevrange/z_prod:clone:running:size/0/-1"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		JSONObject jsonObject = (JSONObject) obj;

		//String name = (String) jsonObject.get("name");
		//System.out.println(name);

		//long age = (Long) jsonObject.get("age");
		//System.out.println(age);

		// loop array
		JSONArray remote = (JSONArray) jsonObject.get("zrevrange");

		Set<String> s = jd.zrevrange("z_prod:clone:running:size", 0, -1);

		for (String dd : s) {
			System.out.println(dd);
		}

		System.out.println("Found Local " + s.size() );
		System.out.println("Found Remote " + remote.size());

		Iterator<String> iterator = remote.iterator();

		while (iterator.hasNext()) {
			String temp = iterator.next();
			if (s.contains(temp))	{
				System.out.println("Found match " + temp);
			} else {
				getValue("zrem/z_prod:clone:running:size/" + temp);
				System.out.println("Not Found " + temp);
			}
		}
		//	iterator = remote.iterator();
		//	while (iterator.hasNext()) {
		//		System.out.println(("List " + iterator.next()));
		//	}





		/**

		for (String dd : s) {
			if (remote.equals(dd)) {
				System.out.println("Found match " + dd);
			} else {
				System.out.println("Not Found " +dd);
			}

		}

		 **/


		//		System.out.println(msg.contains("W_DB_SAPSBXTRG"));


		//Iterator<String> iterator = msg.iterator();





		//while (iterator.hasNext()) {
		//		System.out.println(iterator.next());
		//	}



	}

	public static String getValue (String command) {

		String ret = null;

		try {

			String url = "http://herohead.tzo.net:7379/" + command;
			URL obj = new URL(url);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();
			System.setProperty("http.proxyHost", "proxy.am.mt.mtnet");
			System.setProperty("http.proxyPort", "8080");
			con.setRequestMethod("PUT");


			BufferedReader in = new BufferedReader(
					new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();

			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
			ret = response.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(ret);
		return ret;
	}



}
